﻿using Aliphant.Mvvm.XEvent.Events;
using Aliphant.Mvvm.XEvent.Events.Base.SubScription;
using CommonLibrary.Helper;
using CommonLibrary.UI;
using CuteWallpaperDemo.Managers;
using CuteWallpaperDemo.Models;
using CuteWallpaperDemo.ViewModels;
using DataModel.Contants;
using System;
using System.ComponentModel;
using System.Windows;

namespace CuteWallpaperDemo.Views
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private ProjectAgent _projectAgent;
        private WindowEvent _windowEvent;
        private bool _isLoad = false;

        SubScriptionToken? _subscriptionToken2;
        SubScriptionToken? _subscriptionToken3;
        SubScriptionToken? _subscriptionToken4;
        SubScriptionToken? _subscriptionToken5;
        SubScriptionToken? _subscriptionToken6;
        SubScriptionToken? _subscriptionToken7;

        public MainWindow()
        {
            InitializeComponent();
            this.Title = AppDefines.GetMainWindowTitle;

            _projectAgent = ProjectContainer.ProjectAgent;
            _windowEvent = _projectAgent.EventManager.GetEvent<WindowEvent>();

            //NotifyHelper.AddNewNotify();
            NotifyHelper.Init();
            RegistSubScriptionToken();
        }

        private void RegistSubScriptionToken()
        {
            _subscriptionToken2 = _windowEvent.Subscribe(SaveFileWindow, (p) => p.WindowActionType == WindowActionType.Edit && p.EventId == EventIds.SaveFileDialog);
            _subscriptionToken3 = _windowEvent.Subscribe(OpenAddWindow, (p) => p.WindowActionType == WindowActionType.Open && p.EventId == EventIds.OpenAddWindow);
            _subscriptionToken4 = _windowEvent.Subscribe(OpenViewPictureWindow, (p) => p.WindowActionType == WindowActionType.Open && p.EventId == EventIds.OpenViewPictureWindow);
            _subscriptionToken5 = _windowEvent.Subscribe(SetWindowSize, (p) => p.WindowActionType == WindowActionType.Edit && p.EventId == EventIds.SetWindowSize);
            _subscriptionToken6 = _windowEvent.Subscribe(OpenPlayListPup, (p) => p.WindowActionType == WindowActionType.Open && p.EventId == EventIds.OpenPlayListPup);
            _subscriptionToken7 = _windowEvent.Subscribe(OpenSettings, (p) => p.WindowActionType == WindowActionType.Open && p.EventId == EventIds.OpenSettings);
        }


        private void UnRegistSubScriptionToken()
        {
            _windowEvent.UnSubscribe(_subscriptionToken2);
            _windowEvent.UnSubscribe(_subscriptionToken3);
            _windowEvent.UnSubscribe(_subscriptionToken4);
            _windowEvent.UnSubscribe(_subscriptionToken5);
            _windowEvent.UnSubscribe(_subscriptionToken6);
            _windowEvent.UnSubscribe(_subscriptionToken7);
        }

        private void OpenPlayListPup(WindowEvent @event)
        {
            MyPopup.IsOpen = true;
        }

        private void OpenSettings(WindowEvent @event)
        {
            SettingPop.IsOpen = true;
        }

        private void OpenViewPictureWindow(WindowEvent args)
        {
            WindowBase window = new ViewPictureWindow();
            window.Parameter = args.Data;
            window.Owner = this;
            window.OnShowDialog();
        }



        private void SaveFileWindow(WindowEvent args)
        {
            WindowHelper.SaveFileWindow(args);
        }

        private void OpenAddWindow(WindowEvent args)
        {
            WindowBase window = new SelectWindow();
            window.Parameter = args.Data;
            window.CallBack = args.CallBack;
            window.Owner = this;
            window.OnShowDialog();
        }

        private void SetWindowSize(WindowEvent args)
        {
            if (args.Data is double[] size && size.Length == 2)
            {
                this.Width = size[0] > this.Width ? size[0] : this.Width;
                this.Height = size[1] > this.Height ? size[1] : this.Height;
            }
        }

        protected override void OnActivated(EventArgs e)
        {
            base.OnActivated(e);
            if (!_isLoad)
            {
                _isLoad = true;
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            if (!AppConst.PupExitAlert)
            {
                CleanupAndClose(e);
                return;
            }

            if (ConfigManager.Instance.Settings.IsAlwaysAsk)
            {
                var res = MessageBox.Show("是否关闭？", "CuteWallpaper", MessageBoxButton.YesNo);
                if (res == MessageBoxResult.Yes)
                {
                    CleanupAndClose(e);
                    return;
                }
            }
            CancelClosing(e);
        }

        private void CancelClosing(CancelEventArgs e)
        {
            e.Cancel = true;
            this.Hide();
        }

        private void CleanupAndClose(CancelEventArgs e)
        {
            NotifyHelper.Release();
            UnRegistSubScriptionToken();

            if (this.DataContext is MainWindowViewModel viewModel)
            {
                viewModel.Release();
            }
            base.OnClosing(e);
        }

        private void SubmitButton_Click(object sender, RoutedEventArgs e)
        {
            MyPopup.IsOpen = false;
        }
    }
}
